<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="en">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.0.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32.ico?v=7.0.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16.ico?v=7.0.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.0.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.0.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="贴心的 SpringMVC 原生态地支持 RESTful的设计风格，我们只需稍加简单的注解，就可以轻松搞定 RESTful Web Service。">
<meta name="keywords" content="SpringMVC,RESTful">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringMVC 实现 RESTful Web service">
<meta property="og:url" content="https://blog-test.jetchen.cn/springmvc-restful-web-service/index.html">
<meta property="og:site_name" content="嘎里三分熟">
<meta property="og:description" content="贴心的 SpringMVC 原生态地支持 RESTful的设计风格，我们只需稍加简单的注解，就可以轻松搞定 RESTful Web Service。">
<meta property="og:locale" content="en">
<meta property="og:image" content="http://blogsource.chenkaikai.com/uploads/2017/03/test.png">
<meta property="og:updated_time" content="2019-02-25T15:36:02.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SpringMVC 实现 RESTful Web service">
<meta name="twitter:description" content="贴心的 SpringMVC 原生态地支持 RESTful的设计风格，我们只需稍加简单的注解，就可以轻松搞定 RESTful Web Service。">
<meta name="twitter:image" content="http://blogsource.chenkaikai.com/uploads/2017/03/test.png">






  <link rel="canonical" href="https://blog-test.jetchen.cn/springmvc-restful-web-service/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>SpringMVC 实现 RESTful Web service | 嘎里三分熟</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">嘎里三分熟</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about-me/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>About</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>Archives</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog-test.jetchen.cn/springmvc-restful-web-service/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Jet Chen">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="嘎里三分熟">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SpringMVC 实现 RESTful Web service

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              

              
                
              

              <time title="Created: 2017-03-12 18:05:31" itemprop="dateCreated datePublished" datetime="2017-03-12T18:05:31+08:00">2017-03-12</time>
            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Java-web/" itemprop="url" rel="index"><span itemprop="name">Java web</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><center>贴心的 SpringMVC 原生态地支持 RESTful的设计风格，我们只需稍加简单的注解，就可以轻松搞定 RESTful Web Service。</center><br><a id="more"></a></p>
<p></p><h2>一、Rest 和 RESTful</h2><p>REST 是 Representational State Transfer 的缩写，简单翻译便是表述性状态转移，是由 Roy Thomas Fielding 博士于2000年提出的概念。</p>
<p>其实，REST是Web服务的一种架构风格，再言之，是一种设计风格，也就是一种思想，而并非一种标准。</p>
<p>通常，REST基于使用 HTTP, URI 和 XML, JSON 以及 HTML 这些现有的广泛流行的协议和标准。</p>
<p>而 RESTful，可以简陋翻译成 REST 式，RESTful&nbsp;Web Service是一种常见的REST的应用，是遵守了 REST 风格的 Web 服务，并且是 ROA （面向资源的架构）。</p>
<p>REST 架构之中有一条很重要的原则就是：<span style="background-color: #33cccc;">网络上的所有事物都可以被抽象为资源</span>，理解这一点很关键。</p><h2>二、资源操作</h2><p>当我们欲对一个 User 资源进行 CRUD 操作的时候，最常见的便是直接在 URL 内体现透彻，并且发出的都是 GET 或 POST 请求，如：</p><ul class=" list-paddingleft-2"><li><p><a href="http://127.0.0.1/user/query/1" target="_blank" rel="noopener">http://127.0.0.1/user/query/1</a> GET &nbsp;查询</p></li><li><p><a href="http://127.0.0.1/user/save" target="_blank" rel="noopener">http://127.0.0.1/user/save</a> POST 新增</p></li><li><p><a href="http://127.0.0.1/user/update" target="_blank" rel="noopener">http://127.0.0.1/user/update</a> POST 修改</p></li><li><p><a href="http://127.0.0.1/user/delete" target="_blank" rel="noopener">http://127.0.0.1/user/delete</a> GET/POST 删除</p></li></ul><p>而采用 RESTful 进行编写的时候就会情不自禁采用如下方式：</p><ul class=" list-paddingleft-2"><li><p><a href="http://127.0.0.1/user/1" target="_blank" rel="noopener">http://127.0.0.1/user/1</a> <strong><span style="color: #ff0000;">GET &nbsp;</span></strong>查询</p></li><li><p><a href="http://127.0.0.1/user" target="_blank" rel="noopener">http://127.0.0.1/user</a> &nbsp;<span style="color: #ff0000;"><strong>POST</strong> </span>新增</p></li><li><p><a href="http://127.0.0.1/user" target="_blank" rel="noopener">http://127.0.0.1/user</a> &nbsp;<span style="color: #ff0000;"><strong>PUT</strong> </span>修改</p></li><li><p><a href="http://127.0.0.1/user" target="_blank" rel="noopener">http://127.0.0.1/user</a> &nbsp;<span style="color: #ff0000;"><strong>DELETE</strong></span> 删除</p></li></ul><p>转变点在于，我们的 URI 清晰明朗，所有的请求都围绕着我们的 user 资源，而具体的操作则体现在请求方式和参数上面，后面会详细说明。</p>
<p>另外，响应的设计上面也会有所变动，但是围绕不变的原则点是：</p><ul class=" list-paddingleft-2"><li><p><span style="background-color: #33cccc;">Content body 仅仅用来传输数据</span>，此举在于保证数据拿来即用的原则，而并不需要进行“拆箱”</p></li><li><p>描述数据或者请求的元数据放在 Hearder 中，如 X-Restful-Fields</p></li></ul><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">响应示例：</span><br><span class="line">错误：</span><br><span class="line">&amp;nbsp;&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;status&quot;:&amp;nbsp;200,</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;data&quot;:&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;user_id&quot;:&amp;nbsp;777,</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;user_name&quot;:&amp;nbsp;&quot;Fitz&quot;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&#125;</span><br><span class="line">正确：</span><br><span class="line">&amp;nbsp;&amp;nbsp;Response&amp;nbsp;Headers:</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Status:&amp;nbsp;200</span><br><span class="line">&amp;nbsp;&amp;nbsp;Response&amp;nbsp;Body:</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;user_id&quot;:&amp;nbsp;777,</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;user_name&quot;:&amp;nbsp;&quot;Fitz&quot;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;```&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;h2&gt;三、http 响应状态码&lt;/h2&gt;&lt;p&gt;&lt;img class=&quot;alignnone wp-image-126 size-full&quot; src=&quot;http://blogsource.chenkaikai.com/uploads/2017/03/http-status-code.png&quot; alt=&quot;&quot; height=&quot;706&quot; width=&quot;872&quot;/&gt;&lt;/p&gt;&lt;h2&gt;四、SpringMVC 实现 RESTful Web Service&lt;/h2&gt;&lt;p&gt;其实简言之就是，将 URI 和 HTTP 请求方法映射到 Java&amp;nbsp;处理方法，并将 Java&amp;nbsp;方法处理结果返回给HTTP请求者。</span><br><span class="line"></span><br><span class="line">前面提过只需加以简单注解即可，主要涉及到以下三个注解：&lt;/p&gt;&lt;ul class=&quot; list-paddingleft-2&quot;&gt;&lt;li&gt;&lt;h4&gt;@RequestMapping&lt;/h4&gt;&lt;p&gt;最重要的一个注解，用于处理HTTP请求地址映射，可用于类或方法上，用于类上时表示类中的所有响应请求的方法都是以该地址作为父路径，在Spring中，一般一个Controller类处理一种资源，所以每个Controller类都会加@RequestMapping注解。常用属性：&lt;strong&gt;value&lt;/strong&gt;：指定请求的地址&lt;strong&gt;method&lt;/strong&gt;：指定请求的 method 类型， GET、POST、PUT、DELETE 等&lt;strong&gt;params&lt;/strong&gt;：指定 request 中必须包含某些参数，如果有，才让该方法处理&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;h4&gt;@PathVariable&lt;/h4&gt;&lt;p&gt;映射 URL 路径里面的参数&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;h4&gt;@ResponseBody 和&amp;nbsp;@ResponseBody&lt;/h4&gt;&lt;p&gt;将请求或者响应 body 中的数据（一般常用的为 json）与我们的 Java 对象的互相转换&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;五、代码体现&lt;/h2&gt;&lt;ul class=&quot; list-paddingleft-2&quot;&gt;&lt;li&gt;&lt;p&gt;&lt;strong&gt;保存商品&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;</span><br></pre></td></tr></table></figure><p></p>
<p>/<strong><br>&nbsp;<em>&nbsp;说明：商品操作相关controller<br>&nbsp;</em>&nbsp;/<br>@Controller<br>@RequestMapping(“item”)<br>public&nbsp;class&nbsp;ItemController&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;注入service<br>&nbsp;&nbsp;&nbsp;&nbsp;@Autowired<br>&nbsp;&nbsp;&nbsp;private&nbsp;ItemService&nbsp;itemService;<br>&nbsp;&nbsp;&nbsp;/</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>&nbsp;说明：保存商品<br>&nbsp;&nbsp;&nbsp;&nbsp;</em>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<em>&nbsp;@param&nbsp;item<br>&nbsp;&nbsp;&nbsp;&nbsp;</em>&nbsp;@param&nbsp;desc<br>&nbsp;&nbsp;&nbsp;&nbsp;<em>&nbsp;@return<br>&nbsp;&nbsp;&nbsp;&nbsp;</em>&nbsp;@author&nbsp;CKK<br>&nbsp;&nbsp;&nbsp;&nbsp;*/<br>&nbsp;&nbsp;@RequestMapping(method&nbsp;=&nbsp;RequestMethod.POST)<br>&nbsp;&nbsp;public&nbsp;ResponseEntity<void>&nbsp;saveItem(Item&nbsp;item,&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@RequestParam(value&nbsp;=&nbsp;”desc”)&nbsp;String&nbsp;desc,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@RequestParam(value&nbsp;=&nbsp;”itemParams”)&nbsp;String&nbsp;itemParams)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(StringUtils.isBlank(item.getTitle()))&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;400&nbsp;参数不符<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ResponseEntity.badRequest().build();&nbsp;//&nbsp;TODO&nbsp;参数校验待完善<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boolean&nbsp;saveResult&nbsp;=&nbsp;itemService.saveItem(item,&nbsp;desc,&nbsp;itemParams);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(saveResult)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;201&nbsp;添加成功<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ResponseEntity.status(HttpStatus.CREATED).build();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;500&nbsp;发送异常<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;500&nbsp;发送异常<br>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();<br>&nbsp;&nbsp;}<br>}<figure class="highlight plain"><figcaption><span>class</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">&amp;nbsp;*&amp;nbsp;说明：修改商品</span><br><span class="line">&amp;nbsp;*&amp;nbsp;@param&amp;nbsp;item</span><br><span class="line">&amp;nbsp;*&amp;nbsp;@param&amp;nbsp;desc</span><br><span class="line">&amp;nbsp;*&amp;nbsp;@return</span><br><span class="line">&amp;nbsp;*&amp;nbsp;@author&amp;nbsp;CKK</span><br><span class="line">&amp;nbsp;*/</span><br><span class="line">&amp;nbsp;@RequestMapping(method&amp;nbsp;=&amp;nbsp;RequestMethod.PUT)</span><br><span class="line">&amp;nbsp;public&amp;nbsp;ResponseEntity&lt;Void&gt;&amp;nbsp;updateItem(Item&amp;nbsp;item,&amp;nbsp;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@RequestParam(value&amp;nbsp;=&amp;nbsp;&quot;desc&quot;)&amp;nbsp;String&amp;nbsp;desc,</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@RequestParam(value&amp;nbsp;=&amp;nbsp;&quot;itemParams&quot;)&amp;nbsp;String&amp;nbsp;itemParams)&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(StringUtils.isBlank(item.getTitle()))&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;400&amp;nbsp;参数不符</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;ResponseEntity.badRequest().build();&amp;nbsp;//&amp;nbsp;TODO&amp;nbsp;参数校验待完善</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Boolean&amp;nbsp;saveResult&amp;nbsp;=&amp;nbsp;itemService.updateItem(item,&amp;nbsp;desc,&amp;nbsp;itemParams);</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(saveResult)&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;204&amp;nbsp;添加成功</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;ResponseEntity.status(HttpStatus.NO_CONTENT).build();</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;&amp;nbsp;else&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;500&amp;nbsp;发送异常</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;e)&amp;nbsp;&#123;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.printStackTrace();</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;&#125;</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;500&amp;nbsp;发送异常</span><br><span class="line">&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();</span><br><span class="line">&#125;```&lt;p&gt;但是默认情况下，PUT 请求是无法提交表单数据的，所以我们需要在 web.xml 中添加过滤器来解决，配置如下&lt;code&gt;：&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;</span><br></pre></td></tr></table></figure></void></p>
<!--&nbsp;解决PUT请求无法提交表单数据的问题&nbsp;-->
<p><filter><br>&nbsp;&nbsp;&nbsp;<filter-name>HttpMethodFilter</filter-name><br>&nbsp;&nbsp;&nbsp;<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class><br></filter></p>
<p><filter-mapping><br>&nbsp;&nbsp;&nbsp;<filter-name>HttpMethodFilter</filter-name><br>&nbsp;&nbsp;&nbsp;<url-pattern>/*</url-pattern><br></filter-mapping><figure class="highlight plain"><figcaption><span>class</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">但是还有一点需要注意，DELETE 请求同样是无法提交表单数据的，所以需要在 web.xml 中添加过滤器解决，配置如下：&lt;code&gt;&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;</span><br></pre></td></tr></table></figure></p>
<!--
&nbsp;&nbsp;将POST请求转化为DELETE或者是PUT
&nbsp;&nbsp;要用_method指定真正的请求参数
-->
<p><filter><br>&nbsp;&nbsp;&nbsp;<filter-name>HiddenHttpMethodFilter</filter-name><br>&nbsp;&nbsp;&nbsp;<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class><br></filter></p>
<p><filter-mapping><br>&nbsp;&nbsp;&nbsp;<filter-name>HiddenHttpMethodFilter</filter-name><br>&nbsp;&nbsp;&nbsp;<url-pattern>/*</url-pattern><br></filter-mapping><code>`</code></p><p>当然，测试的时候也有小技巧：</p><p><img class="alignnone wp-image-127 size-full" src="http://blogsource.chenkaikai.com/uploads/2017/03/test.png" alt height="464" width="829">&nbsp;</p>
<p>&nbsp;<br></p><p></p>

      
    </div>

    

    
    
    
	
	<div>
	  
		<div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">------ 本文结束 <i class="fa fa-fighter-jet"></i> 感谢阅读 ------</div>
    
</div>
	  
	</div>

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SpringMVC/" rel="tag"><i class="fa fa-tag"></i> SpringMVC</a>
          
            <a href="/tags/RESTful/" rel="tag"><i class="fa fa-tag"></i> RESTful</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/system-out-printf/" rel="next" title="System.out.printf()">
                <i class="fa fa-chevron-left"></i> System.out.printf()
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/no-need-to-restart-service/" rel="prev" title="使用 thymeleaf 时不重启服务修改 html">
                使用 thymeleaf 时不重启服务修改 html <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Jet Chen</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">68</span>
                    <span class="site-state-item-name">posts</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">33</span>
                    <span class="site-state-item-name">categories</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">182</span>
                    <span class="site-state-item-name">tags</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/goldenJet" title="GitHub &rarr; https://github.com/goldenJet" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://twitter.com/goldenJ51015801" title="Twitter &rarr; https://twitter.com/goldenJ51015801" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.</span> <span class="nav-text">一、Rest 和 RESTful</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">2.</span> <span class="nav-text">二、资源操作</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 – <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Jet Chen</span>

  

  
</div>









<div class="BbeiAn-info">
    <a target="_blank" href="http://www.miitbeian.gov.cn/" rel="nofollow">浙ICP备17005575号-1</a> <!--a标签中增加nofollow属性，避免爬虫出站。-->| 
	<a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=33010802009043" style="text-decoration:none;padding-left:17px;background:url(/images/icon-police.png) no-repeat left center" rel="nofollow">浙公网安备 33010802009043号</a>	  <!--这里将图标作为了背景，以使得能和后面的文字在同一行-->
</div>

        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.0"></script>

  <script src="/js/src/motion.js?v=7.0.0"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.0"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.0"></script>




  
  <script src="/js/src/scrollspy.js?v=7.0.0"></script>
<script src="/js/src/post-details.js?v=7.0.0"></script>



  


  <script src="/js/src/bootstrap.js?v=7.0.0"></script>



  


  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
